#include <REGX52.H>
#include "motor.h"
#include "car.h"
#include "Timer0.h"
#include "Infrared track.h"
#include "Delay.h"
unsigned char message,dtime;
unsigned char t=10;
char mode = 0;
unsigned char PWM=1;
extern bit end;
sbit D1=P1^3;
sbit D2=P1^2;
sbit D3=P1^1;
sbit D4=P1^0;
extern unsigned char Rsave,Lsave,dcompare;
void Uart_Init(void)		//9600bps@11.0592MHz
{
	PCON &= 0x7F;		//波特率不倍速
	SCON = 0x50;		//8位数据,可变波特率
	TMOD &= 0x0F;		//设置定时器模式
	TMOD |= 0x20;		//设置定时器模式
	TL1 = 0xFD;		//设置定时初始值
	TH1 = 0xFD;		//设置定时重载值
	ET1 = 0;		//禁止定时器1中断
	TR1 = 1;		//定时器1开始计时
	PS = 1;				//优先级高
	ES=1;					//打开串口中断
}


	
void control(unsigned char m){
	switch (m)
{
		case '1':
		car_go();
		break;
		case '2':
		car_left();
		break;
		case '3':
		car_stop();
		break;
		case '4':
		car_right();
		break;
		case '5':
		car_oleft();
		break;
		case '6':
		car_oright();
		break;
		case '7':
		car_back();
		break;
		case '8':
		PWM++; //40 60 80档
		if(PWM>=4)PWM=0;
		if(PWM==0){Rsave=42;Lsave=40;t=12;}
		if(PWM==1){Rsave=62;Lsave=60;t=10;}
		if(PWM==2){Rsave=82;Lsave=80;t=8;}
		if(PWM==3){Rsave=97;Lsave=95;t=6;}
		break;
		case '9':
		end=0;
		break;
		case 'A':
		mode=1;
		break;
		case 'B':
		D1=1;D2=1;D3=1;D4=1;
	   mode=0;
	
		break;
		case 'C':
			dtime=10;
		dcompare++;
		if(dcompare>5)dcompare=1;
		if(dcompare==1){
		while(dtime){
			P1_4=1;
			Delay500us();
			P1_4=0;
			Delay(20);
			dtime--;
		}
	}
		if(dcompare==2){
		while(dtime){
			P1_4=1;
			Delay500us();
			Delay500us();
			P1_4=0;
			Delay(19);
			dtime--;
		}
	}if(dcompare==3){
		while(dtime){
			P1_4=1;
			Delay500us();
			Delay500us();
			Delay500us();
			P1_4=0;
			Delay(19);
			dtime--;
		}
	}if(dcompare==4){
		while(dtime){
			P1_4=1;
			Delay500us();
			Delay500us();
			Delay500us();
			Delay500us();
			P1_4=0;
			Delay(18);
			dtime--;
		}
	}
		if(dcompare==5){
		while(dtime){
			P1_4=1;
			Delay500us();
			Delay500us();
			Delay500us();
			Delay500us();
			Delay500us();
			P1_4=0;
			Delay(18);
			dtime--;
		}
	}
		
		break;
		default:
		break;
}
}

void UART_Routine(void) interrupt 4
{
	car_stop();
	RI=0;
	message=SBUF;
	control(message);
}



